package com.mf.controller;

import com.mf.entity.bo.PositionBo;
import com.mf.entity.dto.dept.PositionDto;
import com.mf.entity.request.dept.DeptRequest;
import com.mf.entity.vo.dept.DeptVo;
import com.mf.entity.vo.dept.PositionVo;
import com.mf.service.IPositionService;
import io.github.heollhai.common.config.exceptions.GraceException;
import io.github.heollhai.common.entity.request.publicRequest.UpdateStatusRequest;
import io.github.heollhai.common.enums.EnableOrDisableEnum;
import io.github.heollhai.common.result.Result;
import io.github.heollhai.common.utils.enums.EnumUtils;
import io.github.heollhai.common.utils.tree.TreeUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 岗位表
 *
 * @author mf
 * @since 2024-09-20
 */
@RestController
@RequestMapping("personnel/position/v1")
@Tag(name="岗位")
public class PositionController {
    @Resource
    private IPositionService positionService;

    @Operation(summary = "新增|编辑")
    @PostMapping("save")
    public Result<Integer> save(@Valid @RequestBody() PositionDto position) {
        return Result.success(positionService.insert(position));
    }

    @Operation(summary = "删除")
    @GetMapping("delete/{id}")
    public Result<Integer> delete(@PathVariable("id") Integer id) {
        return Result.success(positionService.deleteById(id));
    }

    @Operation(summary = "修改状态")
    @PostMapping("changeStatus")
    public Result<Boolean> changeStatus(@Valid @RequestBody() UpdateStatusRequest<Integer> updateStatusRequest) {
        if(EnumUtils.existsInEnum(EnableOrDisableEnum.class,updateStatusRequest.getStatus())){
            GraceException.display("状态不合法");
        }
        PositionBo positionBo = new PositionBo();
        positionBo.setId(updateStatusRequest.getId());
        positionBo.setStatus(updateStatusRequest.getStatus());
        return Result.success(positionService.updateById(positionBo));
    }

    @Operation(summary = "根据部门id查询岗位树结构")
    @GetMapping("listFindTreeByDeptId")
    public Result<List<PositionVo>> findByDeptIdTree(@RequestParam("deptId") Integer deptId) {
        return Result.success(TreeUtils.getRoot(positionService.listFindTreeByDeptId(deptId)));
    }

    @Operation(summary = "根据部门id获取平铺树结构")
    @PostMapping("ListFindFlattenTreeByDeptId")
    public Result<List<PositionVo>> flattenTree(@RequestParam("deptId") Integer deptId) {
        return Result.success(TreeUtils.flattenTree(positionService.listFindTreeByDeptId(deptId)));
    }

    @Operation(summary = "查询树结构所有岗位")
    @GetMapping("listFindAllByTerr")
    public Result<List<PositionVo>> listFindAllByTerr() {
        return Result.success(TreeUtils.getRoot(positionService.listFindByEnable()));
    }

    @Operation(summary = "获取平铺树结构")
    @GetMapping("listFindFlattenTree")
    public Result<List<PositionVo>> listFindFlattenTree() {
        return Result.success(TreeUtils.flattenTree(positionService.listFindByEnable()));
    }
}
